package com.whmnx.main.ui.model;

import android.app.Application;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.whmnx.base.bean.OrderBean;
import com.whmnx.base.bean.PingBean2;
import com.whmnx.base.bean.PingTanBean;
import com.whmnx.base.bean.ProductBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.router.RouterActivityPath;
import com.whmnx.base.utils.CodeTimeUtils;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.base.utils.TimeUtils;
import com.whmnx.main.databinding.FragmentCanTeamBinding;
import com.whmnx.main.ui.fragment.CanTeamDetailsFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/9/28 0028 17:42
 * version: 1.0
 * @author Administrator
 */
public class CanTeamModel extends ToolbarViewMainModel {

    private ProductBean productBean;
    private PingBean2 pingBean2;

    public CanTeamModel(@NonNull Application application) {
        super(application);
    }

    private CanTeamDetailsFragment mActivity;
    private FragmentCanTeamBinding mBinding;

    public void initToolbar(CanTeamDetailsFragment canTeamDetailsFragment, FragmentCanTeamBinding binding) {
        this.mActivity = canTeamDetailsFragment;
        this.mBinding = binding;
        setTitleText("拼团详情");
        initView();
    }

    private void initView() {
        PingTanBean pingTanBean = (PingTanBean) mActivity.getIntent().getSerializableExtra("item");
        mBinding.tvTan.setText("还差" + pingTanBean.getGlang() + "人拼团成功");
        productBean = Constants.PRODUCTBEAN;

        for (int i = 0; i < pingTanBean.getGroupUserModel().size(); i++) {
            //是团长
            if (pingTanBean.getGroupUserModel().get(i).getIsColonel() == 1){
                ImageUtil.setImage(mBinding.ivTan,pingTanBean.getGroupUserModel().get(i).getUserInfo_Image());
            }
        }

        Map<String, Object> params = new HashMap<>();
        params.put("gId",pingTanBean.getGId());
        HttpClient.get(mActivity, Constants.SPELLGROUPDETAIL, params,new CallBack<PingBean2>() {
            @Override
            public void onSuccess(PingBean2 result) {
                pingBean2 = result;
                ImageUtil.setImage(mBinding.ivGoods,result.getPImage());
                mBinding.tvGoodsDesc.setText(result.getPName());
                mBinding.tvDesc.setText(result.getPSPEC());
                mBinding.tvMoeny.setText("￥" + result.getGPrice());
                mBinding.tvShang.setText("x"  + result.getNum());

                CodeTimeUtils.payOrderTime(mBinding.tvTime, TimeUtils.string2Millis(result.getEndDateTime()), () -> {
                    mBinding.tvTime.setText("已过期");
                });
            }
        });
    }


    /**
     * 我要参团
     */
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
          /*  if (pingBean2.getGroupUserModel().size() > 0)
           if (pingBean2.getGroupUserModel().get(0).getIsColonel() == 1){
                ToastUtil.toastShortMessage("自己开的团不能参团");
                return;
            }*/
            Map<String, Object> params = new HashMap<>();
            if (pingBean2.getGroupUserModel().size() > 0)
            params.put("orderGroup_No",pingBean2.getGroupUserModel().get(0).getOrderGroup_No());
            params.put("orderGroup_ProductId", productBean.Product_Id);
            params.put("orderGroup_ProductName", productBean.Product_Name);
            params.put("orderGroup_ProductImage", productBean.Product_Image);
            params.put("orderGroup_ProductSPEC", pingBean2.getPSPEC());
            params.put("orderGroup_ProductPrice", pingBean2.getGPrice());
            params.put("orderGroup_ProductNum", pingBean2.getNum());
            params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
            HttpClient.post(mActivity, Constants.SPELLGROUP, params, new CallBack<OrderBean>() {
                @Override
                public void onSuccess(OrderBean result) {
                    Constants.ORDERBEAN = result;
                    ARouter.getInstance().build(RouterActivityPath.Home.COM).navigation();
                }
            });
        }
    });
}
